/*
* Copyright (C) 2008-2009 J-P Nurmi jpnurmi@gmail.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* $Id: nicklistview.cpp 284 2009-01-12 20:18:50Z jpnurmi $
*/

#include "nicklistview.h"
#include <QContextMenuEvent>
#include <QAction>
#include <QMenu>

NickListView::NickListView(QWidget* parent)
    : QListView(parent)
{
    connect(this, SIGNAL(doubleClicked(QModelIndex)),
        this, SLOT(onDoubleClicked(QModelIndex)));
}

void NickListView::contextMenuEvent(QContextMenuEvent* event)
{
    QModelIndex index = indexAt(event->pos());
    if (index.isValid())
    {
        QString nick = index.data(Qt::EditRole).toString();

        QMenu menu(this);
        QAction* action = 0;

        action = menu.addAction(tr("&Whois"), this, SLOT(onActionTriggered()));
        action->setData(QString("WHOIS %1").arg(nick));

        action = menu.addAction(tr("&Version"), this, SLOT(onActionTriggered()));
        action->setData(QString("VERSION %1").arg(nick));

        action = menu.addAction(tr("&Ping"), this, SLOT(onActionTriggered()));
        action->setData(QString("PING %1").arg(nick));

        menu.addSeparator();
        QMenu* modeMenu = menu.addMenu("&Mode");

        action = modeMenu->addAction(tr("&Op"), this, SLOT(onActionTriggered()));
        action->setData(QString("MODE %2 +o %1").arg(nick));

        action = modeMenu->addAction(tr("&Deop"), this, SLOT(onActionTriggered()));
        action->setData(QString("MODE %2 -o %1").arg(nick));

        action = modeMenu->addAction(tr("&Voice"), this, SLOT(onActionTriggered()));
        action->setData(QString("MODE %2 +v %1").arg(nick));

        action = modeMenu->addAction(tr("D&evoice"), this, SLOT(onActionTriggered()));
        action->setData(QString("MODE %2 -v %1").arg(nick));

        QMenu* kickMenu = menu.addMenu("&Kick / Ban");

        action = kickMenu->addAction(tr("&Kick"), this, SLOT(onActionTriggered()));
        action->setData(QString("KICK %2 %1").arg(nick));

        action = kickMenu->addAction(tr("&Ban"), this, SLOT(onActionTriggered()));
        action->setData(QString("BAN %2 %1").arg(nick));

        menu.exec(event->globalPos());
    }
}

void NickListView::onDoubleClicked(const QModelIndex& index)
{
    const QString nick = index.data(Qt::EditRole).toString();
    emit commandRequested(QString("QUERY %1").arg(nick));
}

void NickListView::onActionTriggered()
{
    QAction* action = qobject_cast<QAction*>(sender());
    Q_ASSERT(action);
    emit commandRequested(action->data().toString());
}
